package com.bootdo.common.utils;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.google.gson.Gson;
import org.jsoup.Connection;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: ZouLF
 * @Date: 2018/5/29 10:49
 * @Description: 短信验证码（聚合通道）
 */
public class JuSms {


    public static boolean sendSms(String mobile,String code) throws IOException {
        String code0 = java.net.URLDecoder.decode("#code#="+code, "utf-8");
        Map<String,String> map = new HashMap<String,String>();
        map.put("mobile",mobile);
        map.put("tpl_id","148769");
        map.put("tpl_value",code0);
        map.put("key", "0f9cc76af575681d94db3723f372fb5d");
        map.put("dtype","json");
        Connection.Response response = HttpUtils.post("http://v.juhe.cn/sms/send", map);
        String result = java.net.URLDecoder.decode(response.body(), "utf-8");
        //return R.ok().put("data",result);
        Gson gson = new Gson();
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap = gson.fromJson(result, map.getClass());//关键
//        String goodsid=(String) map.get("goods_id");
//        System.out.println("map的值为:"+goodsid);
//        JSONObject jasonObject = JSONObject.fromObject(result);
//        Map<String,Object> jsonMap = (Map<String,Object>)jasonObject;
        String error_code = jsonMap.get("error_code").toString();
        String reason = jsonMap.get("reason").toString();
        System.out.println("error_code::"+error_code);
        System.out.println("reason::"+reason);
        if ("0".equals(error_code)) {
            return true;
        } else {
           return false;
        }
    }

    /*
    * 发货短信
    * */
    public static boolean sendFSms(String mobile,String code) throws IOException {
        String code0 = java.net.URLDecoder.decode("#code#="+code, "utf-8");
        Map<String,String> map = new HashMap<String,String>();
        map.put("mobile",mobile);
        map.put("tpl_id","157444");
        map.put("tpl_value",code0);
        map.put("key", "0f9cc76af575681d94db3723f372fb5d");
        map.put("dtype","json");
        Connection.Response response = HttpUtils.post("http://v.juhe.cn/sms/send", map);
        String result = java.net.URLDecoder.decode(response.body(), "utf-8");
        //return R.ok().put("data",result);
        Gson gson = new Gson();
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap = gson.fromJson(result, map.getClass());//关键
//        String goodsid=(String) map.get("goods_id");
//        System.out.println("map的值为:"+goodsid);
//        JSONObject jasonObject = JSONObject.fromObject(result);
//        Map<String,Object> jsonMap = (Map<String,Object>)jasonObject;
        String error_code = jsonMap.get("error_code").toString();
        String reason = jsonMap.get("reason").toString();
        System.out.println("error_code::"+error_code);
        System.out.println("reason::"+reason);
        if ("0".equals(error_code)) {
            return true;
        } else {
            return false;
        }
    }

}
